package com.moyun.finance.service;

import com.moyun.finance.entity.*;
import com.moyun.finance.repository.AccountRepository;
import com.moyun.finance.utils.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccountService {
    @Autowired
    AccountRepository ar;
    @Autowired
    BillService bs;

    public Response save(Account account){
        account.setId(BaseUtil.uuid());
        account.setNowMoney(account.getInitialMoney());
        ar.save(account);
        return new Response("添加成功!",true);
    }

    public Response update(Account account){
        ar.save(account);
        return new Response("账户信息修改成功!",true);
    }

    public Response delete(Account account){
        ar.delete(account);
        return new Response("账户删除成功",true);
    }

    //本公司所有账户
    public List<Account> findAll(String companyId){
        List<Account> accounts =  ar.findByCompanyId(companyId);
        return accounts;
    }

    public Account findOneById(String accountId){
        return ar.findById(accountId).orElse(null);
    }

    //账户间转账
    public Response transfer(Transfer t){
        Account inA = t.getInAccount();
        Account outA = t.getOutAccount();
        BigDecimal tMoney = new BigDecimal(t.getMoney());
        Bill bill = new Bill();

        //出账
        BigDecimal outANowMoney = new BigDecimal(outA.getNowMoney());
        BigDecimal outAOutMoney = new BigDecimal(outA.getOutMoney());
        outA.setNowMoney(outANowMoney.subtract(tMoney).doubleValue());
        outA.setOutMoney(outAOutMoney.add(tMoney).doubleValue());
        bill.setAccountId(outA.getId());
        bill.setRemark(t.getRemark());
        bill.setCreateDate(t.getDate());
        bill.setBillType(0);
        bill.setBillMoney(t.getMoney());
        bill.setCompanyId(t.getCompanyId());
        bs.save(bill);
        ar.save(outA);


        //入账
        BigDecimal inANowMoney = new BigDecimal(inA.getNowMoney());
        BigDecimal inAInMoney = new BigDecimal(inA.getInMoney());
        inA.setNowMoney(inANowMoney.add(tMoney).doubleValue());
        inA.setInMoney(inAInMoney.add(tMoney).doubleValue());
        bill.setAccountId(inA.getId());
        bill.setBillType(1);
//        ar.save(inA);
        bs.save(bill);
        ar.save(inA);

        return new Response("转账成功!",true);
    }

    public Map<String,Double> count(String companyId){
        Map<String,Double> countMap = new HashMap<>();
        List<Account> as = findAll(companyId);
        BigDecimal nowMoney = new BigDecimal(0);
        BigDecimal outMoney = new BigDecimal(0);
        BigDecimal inMoney = new BigDecimal(0);
        for(Account a : as){
            BigDecimal anowMoney = new BigDecimal(a.getNowMoney());
            BigDecimal aoutMoney = new BigDecimal(a.getOutMoney());
            BigDecimal ainMoney = new BigDecimal(a.getInMoney());
            nowMoney = nowMoney.add(anowMoney);
            outMoney = outMoney.add(aoutMoney);
            inMoney = inMoney.add(ainMoney);
        }
        countMap.put("nowMoney", nowMoney.doubleValue());
        countMap.put("outMoney", outMoney.doubleValue());
        countMap.put("inMoney", inMoney.doubleValue());
        return countMap;
    }
}
